<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑菜单</title>
    <link rel="stylesheet" href="../../static/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../static/css/common.css"/>
</head>
<body class="body" style="padding: 30px 50px;">
<div id="app">
  <form class="layui-form" lay-filter="form">
    <input name="id" style="display: none">
    <div class="layui-row layui-col-space10">
      <div class="layui-col-sm6">
        <div class="layui-form-item">
          <div class="layui-form-label">上级目录</div>
          <div class="layui-inline">
            <input v-model="state.parentId" type="text" id="tree" name="parentId" lay-verify="required" lay-filter="tree" placeholder="请选择上级目录" class="layui-input layui-input-form">
          </div>
        </div>
      </div>
      <div class="layui-col-sm6">
        <div class="layui-form-item">
          <div class="layui-form-label">菜单排序</div>
          <div class="layui-inline">
            <input class="layui-input" lay-verify="required|number" name="sort" placeholder="请输入菜单排序">
          </div>
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-form-label">菜单类型</div>
      <div class="layui-inline">
        <input type="radio" lay-filter="type" name="type" value="1" title="菜单" checked>
        <input type="radio" lay-filter="type" name="type" value="2" title="页面">
        <input type="radio" lay-filter="type" name="type" value="3" title="按钮">
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-form-label">菜单名称</div>
      <div class="layui-inline">
        <input class="layui-input layui-input-form" lay-verify="required" name="name" placeholder="请输入菜单名称">
      </div>
    </div>
    <div v-if="state.isPage" class="layui-form-item">
      <div class="layui-form-label">菜单路径</div>
      <div class="layui-inline">
        <input class="layui-input layui-input-form" lay-verify="required" name="path" placeholder="请输入菜单路径">
      </div>
    </div>
    <div v-if="state.isButton" class="layui-form-item">
      <div class="layui-form-label">权限标识</div>
      <div class="layui-inline">
        <input class="layui-input layui-input-form" lay-verify="required" name="permission" placeholder="请输入权限标识">
      </div>
    </div>
    <div class="layui-form-item btn-div">
      <div class="layui-inline">
        <button type="button" class="layui-btn layui-btn-sm layui-btn-primary btn-save" @click="cancel">取消</button>
        <button type="button" class="layui-btn layui-btn-sm layui-btn-normal btn-save" lay-submit lay-filter="form" @click="save">保存</button>
      </div>
    </div>
  </form>
</div>
</body>
<script src="../../static/layui/layui.js"></script>
<script src="../../static/js/axios.js"></script>
<script src="../../static/js/http.js"></script>
<script src="../../static/js/vue3.js"></script>
<script src="../../static/js/common.js"></script>
<script>
  const app = {
    setup(){
      const state = Vue.reactive({
        parentId: '',
        isPage: false,
        isButton: false
      })

      const getParentTree = (parentId) => {
        get('/api/sys/menu/select/tree', null).then(res => {
          if (res.success){
            Vue.nextTick(() => {
              layui.config({
                base: '../../static/js/'
              }).use('treeSelect', function () {
                const treeSelect = layui.treeSelect
                renderParentTree(treeSelect, res.data, parentId)
              })
            })
          } else {
            errorMsg(res.msg)
          }
        })
      }

      const renderParentTree = (treeSelect, data, parentId) => {
        treeSelect.render({
          elem: '#tree',
          data: data,
          style: {
            folder: {
              enable: true
            },
            line: {
              enable: true
            }
          },
          search: true,
          click: function(obj){
            state.parentId = obj.current.id
          },
          success: function (data) {
            //  默认为0，即顶级目录
            parentId = parentId ? parentId : 0
            state.parentId = parentId
            treeSelect.checkNode('tree', parentId)
          }
        })
      }

      const save = () => {
        Vue.nextTick(() => {
          const form = layui.form
          form.on('submit(form)', function (data) {
            post('/api/sys/menu/edit', data.field).then(res => {
              if (res.success){
                successMsg(res.data)
                window.parent.getChild()
                cancel()
              } else {
                errorMsg(res.msg)
              }
            })
            return false
          })
        })
      }
      const cancel = () => {
        parent.layer.closeAll()
      }

      Vue.nextTick(() => {
        const form = layui.form
        form.on('radio(type)', function (data) {
          state.isPage = data.value === '2' || data.value === '3';
          state.isButton = data.value === '3'
        })
      })

      return {
        state,
        getParentTree,
        save,
        cancel
      }
    }
  }
  const vue = Vue.createApp(app).mount('#app')

  //  获取父页面的传值
  function getParentData(data) {
    if (data && data.id){
      const form = layui.form
      if (data.type === '2' || data.type === '3'){
        vue.state.isPage = true
      }
      if (data.type === '3'){
        vue.state.isButton = true
      }
      setTimeout(function () {
        form.val('form', data)
      }, 1)
    }
    vue.getParentTree(data.parentId)
  }
</script>
<style>

</style>
</html>